<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@include file="/static/common/taglib.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>出乘提示</title>
    <%@include file="../../public/commonCss.jsp" %>
    <%@include file="../../public/commonJs.jsp" %>
    <link rel="stylesheet" type="text/css" href="${ctx}/static/css/message.css"/>
    <script type="text/javascript" src="${ctx}/static/ueditor/ueditor.config.js"></script>
    <!-- 编辑器源码文件 -->
    <script type="text/javascript" src="${ctx}/static/ueditor/ueditor.all.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="${ctx }/static/ueditor/ueditor.parse.min.js"></script>

</head>
<body ms-controller="main" class="ms-controller">
<div id="wrapper">
    <div class="boxmain whiteBox lrbox">
        <div class="lrboxleft borderR" style="width:25%">
            <div class="from-header borderB contentbox">
                <div>
                    <span class="font14"> 所属分类:</span><a id="category"><span class=" fontOrange2">提示模板</span></a>
                    <button class="layui-btn layui-btn-small" style="float: right;margin-left: 10px"
                            onclick="buildTemplate()">新增模板
                    </button>
                    <button class="layui-btn layui-btn-small layui-btn-normal" style="float: right"
                            onclick="showTemplate()">预览
                    </button>
                </div>
            </div>
            <div class="from-body withheader contentbox" style=" overflow: auto;">
                <div class="box" ms-repeat="tableData">
                    <div class="box-tit">
                        <span>{{$index+1}}、</span>
                        更新时间：<span style="color:red;">{{el.modifyTime}}</span>
                        <div class="layui-btn-group fr">
                            <button class="layui-btn layui-btn-mini" ms-if="!$first"
                                    title="上移" ms-on-click="moveTempLate(el,$index,-1)">
                                <i class="layui-icon">&#xe619;</i>
                            </button>
                            <button class="layui-btn layui-btn-mini" ms-if="!$last"
                                    ms-on-click="moveTempLate(el,$index,1)"
                                    title="下移">
                                <i class="layui-icon">&#xe61a;</i>
                            </button>
                            <button class="layui-btn layui-btn-mini layui-btn-primary"
                                    ms-if="el.isDelete==1"
                                    ms-on-click="deleteTemplate(el)"
                                    title="删除">
                                <i class="layui-icon">&#xe640;</i>
                            </button>
                        </div>
                    </div>
                    <div class="box-con">
                        <p class="title">{{el.title|truncate(20)}}</p>
                        <p>&nbsp;&nbsp;&nbsp;&nbsp;{{el.contentText|truncate(60)}}</p>
                        <div class="mask" ms-on-click="showMsg(el)" ms-if="el.isDelete==1"><span></span></div>
                    </div>
                    <div class="box-fj">
                    </div>
                </div>
            </div>
        </div>
        <div class="lrboxmain borderL" style="margin-left:25%">
            <div class="from-header borderB">
                <span class="font14">发布公告</span>
            </div>
            <div class="from-body withheader contentbox8 layui-form layui-form-pane" style=" overflow: auto;">
                <input type="hidden" name="id">
                <div style="width: 100%;;float: left">
                    <div class="layui-form-item tt2">
                        <label class="layui-form-label">标题</label>
                        <div class="layui-input-block">
                            <input type="text" style="width: 100%" placeholder="请输入标题" lay-verify="required"
                                   class="layui-input t2"
                                   name="title">
                        </div>
                    </div>
                    <div class="layui-form-item tt2">
                        <textarea id="container" style="width: 100%; height: 400px; margin: 0 auto;">
                        </textarea>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit="" lay-filter="demo1">保存</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- whitebox -->
</div>
<script type="text/javascript">
    var vm;
    var ue;
    var form = layui.form();
    avalon.ready(function () {
        vm = avalon.define({
            $id: "main",
            tableData: []
        });
        avalon.scan();
    });
    $(function () {
        loadTableData();
        ue = UE.getEditor("container");
        //监听提交
        form.on('submit(demo1)', function (data) {
            data.field["contentText"] = ue.getContentTxt();
            data.field["contentHtml"] = ue.getContent();
            console.log(data.field);
            $.ajax({
                type: 'post',
                url: '${ctx}/trainNoticeController/saveOrUpdate.action',
                data: data.field,
                dataType: "json",
                timeout: 10000,
                success: function (data) {
                    if (data.code == 0) {
                        window.location.reload();
                    } else {
                        layui.layer.msg('保存失败，请重试', {icon: 2});
                    }
                }
            });
            return false;
        });

    });

    //构建新公告
    function buildTemplate() {
        window.location.reload();
    }

    //移动模板
    function moveTempLate(el, index, type) {
        var dataStr = {
            id1: el.$model.id,
            id2: vm.$model.tableData[index + type].id
        }
        ajaxUtil.post("${ctx}/trainNoticeController/moveTemplate.action", dataStr, true, function (data) {
            loadTableData();
        })
    }

    //显示消息
    function showMsg(el) {
        $("input[name='id']").val(el.$model.id);
        $("input[name='title']").val(el.$model.title);
        ue.setContent(el.$model.contentHtml);
    }

    //预览
    function showTemplate() {
        parent.layui.layer.open({
            type: 2,
            title: "出乘提示预览",
            shadeClose: true,
            shade: 0.2,
            maxmin: true, //开启最大化最小化按钮
            area: ['400px', '90%'],
            content: '${ctx}/trainNoticeController/show'
        });
    }

    //加载表格数据
    function loadTableData() {
        $.ajax({
            type: 'post',
            url: '${ctx}/trainNoticeController/selectAllTemplate.action',
            data: "",
            dataType: "json",
            timeout: 10000,
            success: function (data) {
                if (data.code == 0) {
                    vm.tableData = data.tableData;
                }
            }
        });
    }

    //删除
    function deleteTemplate(el) {
        layui.layer.alert('你确定删除该模板吗?', {
            title: '提醒',
            btn: ['确定', '取消']
        }, function () {
            //执行删除的ajax
            $.ajax({
                type: 'post',
                url: '${ctx}/trainNoticeController/deleteTemplate.action?id=' + el.$model.id,
                data: "",
                dataType: "json",
                timeout: 10000,
                success: function (data) {
                    if (data.code == 0) {
                        layer.msg('删除成功', {icon: 1});
                        loadTableData();
                    } else if (data.code == -1) {
                        layer.msg('删除失败,请重试', {icon: 2});
                    }

                }
            });
        });
    }

    //删除附件
    function deleteContentFile() {
        $("#contentFile").hide();
        $("input[name='contentFile']").val("");
        $("input[name='contentFileName']").val("");
    }
</script>
</body>

</html>